import React, { Component } from 'react';
import { connect } from 'dva';
import moment from 'moment';
import { Modal, Icon, Input, Form, message } from 'antd';
import * as menuService from '../../services/menus';
const FormItem = Form.Item;

class ChangePassword extends Component {
    constructor(props) {
        super(props);
    }
    handleOk = (e) => {
        let oldPassword = ""
        let newPassword = ""
        let okPwd = ""
        this.props.form.validateFields((err, values) => {
            if (!err) {
                oldPassword = values.oldPassword
                newPassword = values.newPassword
                okPwd = values.okNewPassword
            }
        })
        if (newPassword !== okPwd) {
            message.error("新密码和确认密码不一致！")
            return
        } 
        if (newPassword === "00000000" && okPwd === "00000000") {
            message.error("新密码不能为初始化密码！")
            return
        }
        if(newPassword.length<6){
            message.error("新密码长度不能小于6位,请重新输入!")
            return;
        }
        if (this.PassWordStrong(newPassword)) {
            message.error("密码强度不够,请重新输入！")
            return;
        }
        menuService.changePassword({ oldPassword, newPassword }
        ).then(function (data) {
            if (data === 'ok') {
                message.success("密码修改成功！")
                this.props.dispatch({
                    type: 'menus/hidePwd'
                });
            }

        }.bind(this)).catch(function (ex) {
            message.error(ex.toString())
        });
    }
    CharMode(inputChar){
        if (inputChar >= 48 && inputChar <= 57) {
            return 1; //数字
        } else if (inputChar >= 65 && inputChar <= 90) {
            return 2; //字母（小写）
        } else if (inputChar >= 97 && inputChar <= 122) {
            return 2; //字母（大写）
        } else {
            return 3; //特殊字符
        }
    }
    PassWordStrong(password) {
        let pwdChars = password.split('');
        let initvalue = this.CharMode(pwdChars.shift().charCodeAt(0));
        for (let i = 0; i < pwdChars.length; i++) {
            let ch = pwdChars[i];
            let chIndex = ch.charCodeAt(0);
            if (initvalue != this.CharMode(chIndex)) {
                return false;
            }
            initvalue = this.CharMode(chIndex);
        }
        return true;
    }
    handleCancel = (e) => {
        this.props.dispatch({
            type: 'menus/hidePwd'
        });
    }
    render() {
        const { getFieldDecorator } = this.props.form;
        const { iscsh } = this.props;
        let title = iscsh === "true" ? "初始化密码，请修改" : "修改密码"
        return (
            <div>
                <Modal
                    title={title}
                    visible={this.props.displayPwd}
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                >
                    <Form horizontal onSubmit={this.okHandler}>
                        <FormItem
                            label="原密码"
                        >
                            {getFieldDecorator('oldPassword', {
                                rules: [{ required: true, message: '请输入原密码！' }],
                            })(
                                <Input prefix={<Icon type="lock" style={{ fontSize: 13 }} />} type="password" placeholder="原密码" />
                            )}
                        </FormItem>

                        <FormItem
                            label="新密码"
                        >
                            {getFieldDecorator('newPassword', {
                                rules: [{ required: true, message: '请输入新密码！' }],
                            })(
                                <Input prefix={<Icon type="lock" style={{ fontSize: 13 }} />} type="password" placeholder="新密码" />
                            )}
                        </FormItem>
                        <FormItem
                            label="确认密码"
                        >
                            {getFieldDecorator('okNewPassword', {
                                rules: [{ required: true, message: '请输入确认密码！' }],
                            })(
                                <Input prefix={<Icon type="lock" style={{ fontSize: 13 }} />} type="password" placeholder="确认密码" />
                            )}
                        </FormItem>
                        <font style={{color:"red",fontSize: 11.6}}>
                        注:新密码长度不能小于6位，不允许有空格；数字、字母以及特殊字符三者之间必须含有两种！</font>
                    </Form>
                </Modal>
            </div>
        );
    }
}
function mapStateToProps(state) {
    const { displayPwd, iscsh } = state.menus
    //console.log(state.menus)
    return {
        displayPwd,
        iscsh
    }
}
export default Form.create()(connect(mapStateToProps)(ChangePassword));
